package com.sofwin.ruoyi.scyffinal.controller;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.api.RemoteDeptService;
import com.ruoyi.system.api.domain.SysDept;
import com.sofwin.ruoyi.scyffinal.pojo.Region;
import com.sofwin.ruoyi.scyffinal.service.RegionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
/**
 * 区域管理控制器类
 *
 * 提供区域信息的RESTful API接口，包括区域的增删改查操作。
 * 继承BaseController，集成了Ruoyi框架的基础控制器功能。
 *
 * @author liuxingchen
 * @since 2025/9/30
 */

@RestController
@RequestMapping("/region")
@Slf4j
public class RegionController extends BaseController {
    @Autowired
    private RegionService regionService;

    @Autowired
    private RemoteDeptService remoteDeptService;

    /**
     * 查询
     */
    @GetMapping("/select")
    @RequiresPermissions("system:region:list")
    public TableDataInfo selectAll() {
        startPage();
        List<Region> list = regionService.list();
        return getDataTable(list);
    }
    @GetMapping("/testRemote")
    public AjaxResult testRemote() {
        startPage();
        SysDept sysDept = new SysDept();
//        sysDept.setDeptName("若依科技");
        R<List<SysDept>> list = remoteDeptService.list();
        return success(list);

    }
    /**
     * 添加
     */
    @PostMapping("/add")
    @RequiresPermissions("system:region:add")
    @Log(title = "区域管理", businessType = BusinessType.INSERT)
    public AjaxResult insert(@Validated @RequestBody Region region) {
        boolean success = regionService.save(region);
        return success ?
                AjaxResult.success("区域添加成功", region) :
                AjaxResult.error("区域添加失败");
    }
    /**
     * 修改
     */
    //先根据id查询
    @GetMapping("/{id}")
    @RequiresPermissions("system:region:query")
    public AjaxResult selectById(@PathVariable Integer id) {
        log.info("查询ID为{}的区域信息", id);
        Region region = regionService.getById(id);
        if (region == null) {
            return AjaxResult.error("未找到ID为" + id + "的区域");
        }
        return AjaxResult.success(region);
    }
    @PutMapping("/{id}")
    @RequiresPermissions("system:region:edit")
    @Log(title = "区域管理", businessType = BusinessType.UPDATE)
    public AjaxResult update(@PathVariable Integer id, @Validated @RequestBody Region region) {
        if (!id.equals(region.getId())) {
            return AjaxResult.error("路径ID与实体ID不一致");
        }
        log.info("修改ID为{}的区域信息: {}", id, region);
        boolean success = regionService.updateById(region);
        return success ?
                AjaxResult.success("区域修改成功") :
                AjaxResult.error("区域修改失败或区域不存在");
    }

    /**
     * 删除
     */
    @DeleteMapping("/{id}")
    @RequiresPermissions("system:region:remove")
    @Log(title = "区域管理", businessType = BusinessType.DELETE)
    public AjaxResult delete(@PathVariable Integer id) {
        log.info("删除ID为{}的区域信息", id);
        boolean success = regionService.removeById(id);
        return success ?
                AjaxResult.success("区域删除成功") :
                AjaxResult.error("区域删除失败或区域不存在");
    }

}
